home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue48 / Clinic / MonHelpU.pas next >
Encoding:
Pascal/Delphi Source File  |  1999-05-19  |  3.0 KB  |  123 lines

  1. unit MonHelpU;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows;
  7.  
  8. const
  9.   TraceBufSize = 8192;
  10.  
  11. var
  12.   wm_ClientLibNotify: Cardinal = 0;
  13.   wm_MonitorNotify: Cardinal = 0;
  14.  
  15. type
  16.   TWMClientLibNotify = record
  17.     Msg: Cardinal;
  18.     Wnd: HWnd;
  19.     Unused: Longint;
  20.     Result: Longint;
  21.   end;
  22.  
  23.   TWMMonitorNotify = record
  24.     Msg: Cardinal;
  25.     Wnd: HWnd;
  26.     TraceValue: Integer;
  27.     Result: Longint;
  28.   end;
  29.  
  30. var
  31.   wm_UpdateClients: Cardinal = 0;
  32.  
  33. type
  34.   TWMUpdateClients = record
  35.     Msg: Cardinal;
  36.     TraceValue: Longint;
  37.     Unused: Longint;
  38.     Result: Longint;
  39.   end;
  40.  
  41. var
  42.   wm_TraceMessage: Cardinal = 0;
  43.  
  44. //These classes come from the IPCDemos sample project
  45. type
  46. { This is a generic class for all encapsulated WinAPI's which need to call
  47.   CloseHandle when no longer needed.  This code eliminates the need for
  48.   3 identical destructors in the TEvent, TMutex, and TSharedMem classes
  49.   which are descended from this class. }
  50.   THandledObject = class(TObject)
  51.   protected
  52.     FHandle: THandle;
  53.   public
  54.     destructor Destroy; override;
  55.     property Handle: THandle read FHandle;
  56.   end;
  57.  
  58. { This class simplifies the process of creating a region of shared memory.
  59.   In Win32, this is accomplished by using the CreateFileMapping and
  60.   MapViewOfFile functions. }
  61.   TSharedMem = class(THandledObject)
  62.   private
  63.     FName: string;
  64.     FSize: Integer;
  65.     FCreated: Boolean;
  66.     FFileView: Pointer;
  67.   public
  68.     constructor Create(const Name: string; Size: Integer);
  69.     destructor Destroy; override;
  70.     property Name: string read FName;
  71.     property Size: Integer read FSize;
  72.     property Buffer: Pointer read FFileView;
  73.     property Created: Boolean read FCreated;
  74.   end;
  75.  
  76. implementation
  77.  
  78. uses
  79.   SysUtils;
  80.  
  81. { THandledObject }
  82.  
  83. destructor THandledObject.Destroy;
  84. begin
  85.   if FHandle <> 0 then
  86.     CloseHandle(FHandle);
  87. end;
  88.  
  89. { TSharedMem }
  90.  
  91. constructor TSharedMem.Create(const Name: string; Size: Integer);
  92. begin
  93.   try
  94.     FName := Name;
  95.     FSize := Size;
  96.     { CreateFileMapping, when called with $FFFFFFFF for the hanlde value,
  97.       creates a region of shared memory }
  98.     FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0,
  99.         Size, PChar(Name));
  100.     if FHandle = 0 then abort;
  101.     FCreated := GetLastError = 0;
  102.     { We still need to map a pointer to the handle of the shared memory region }
  103.     FFileView := MapViewOfFile(FHandle, FILE_MAP_WRITE, 0, 0, Size);
  104.     if FFileView = nil then abort;
  105.   except
  106.     raise Exception.CreateFmt('Error creating shared memory %s (%d)', [Name, GetLastError]);
  107.   end;
  108. end;
  109.  
  110. destructor TSharedMem.Destroy;
  111. begin
  112.   if FFileView <> nil then
  113.     UnmapViewOfFile(FFileView);
  114.   inherited Destroy;
  115. end;
  116.  
  117. initialization
  118.   wm_ClientLibNotify := RegisterWindowMessage('wm_ClientLibNotify');
  119.   wm_MonitorNotify := RegisterWindowMessage('wm_MonitorNotify');
  120.   wm_UpdateClients := RegisterWindowMessage('wm_UpdateClients');
  121.   wm_TraceMessage := RegisterWindowMessage('wm_TraceMessage');
  122. end.
  123.